package com.sxpi.zixun.controller.adminrole;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxpi.zixun.bo.AdminRoleBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.controller.BaseController;
import com.sxpi.zixun.local.UserThreadLocal;
import com.sxpi.zixun.pojo.AdminRole;
import com.sxpi.zixun.service.adminrole.IAdminRoleService;
import com.sxpi.zixun.service.adminuser.IAdminUserService;
import com.sxpi.zixun.vo.AdminRolePermissionVo;
import com.sxpi.zixun.vo.AdminRoleQueryVo;
import com.sxpi.zixun.vo.AdminRoleVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.pug.generator.anno.PugDoc;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/8/25 11:00
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@PugDoc(name = "后台角色管理", tabname = "kss_roles")
public class AdminRoleController extends BaseController {

    private final IAdminRoleService adminroleService;

    private final IAdminUserService adminUserService;

    /**
     * 查询后台角色管理列表信息
     * @return
     */
    @PostMapping("/adminrole/list")
    @PugDoc(name = "查询后台角色管理列表信息")
    public List<AdminRoleBo> findAdminRoleList(){
        return adminroleService.findAdminRoleList();
    }

    /**
     * 查询后台角色管理列表信息
     * 这里主要拿的是auth和AdminRoleController中的findAdminRoleList非常相似，只是有个判断auth的地方
     *
     * 把找到的用户角色中的属性auth设置为true
     * @return
     */
    @PostMapping("/adminrole/user/role")
    @PugDoc(name = "查询用户对应的角色信息")
    public List<AdminRoleBo> findAdminRoleUserList(@RequestParam("userId")Long userId){
        // 查询所有角色信息
        List<AdminRoleBo> adminRoleList = adminroleService.findAdminRoleList();// [1,2,3,4]
        // 查询某个用户对应的角色信息
        List<Long> byUserPermission = adminUserService.findByUserRikeIds(userId);// [1,2]
        return adminRoleList.stream().map(role->{
            // 用户下面有绑定了多少角色
            long count = byUserPermission.stream().filter(roleId -> {
                return role.getId().equals(roleId);
            }).count();
            // false未授权 true授权
            role.setAuth(count > 0);
            return role;
        }).collect(Collectors.toList());
    }

    /**
     * 查询后台角色管理列表信息并分页
     * @param adminRoleVo
     * @return
     */
    @PostMapping("/adminrole/load")
    @PugDoc(name = "查询后台角色管理列表信息并分页")
    public IPage<AdminRoleBo> findAdminRoles(@RequestBody AdminRoleVo adminRoleVo){
        return adminroleService.findAdminRolePage(adminRoleVo);
    }

    /**
     * 保存和修改后台角色管理
     * @param adminrole
     * @return
     */
    @PostMapping("/adminrole/saveupdate")
    @PugDoc(name = "保存和修改后台角色管理")
    public AdminRoleBo saveUpdateAdminRole(@RequestBody AdminRole adminrole) {
        return adminroleService.saveUpdateAdminRole(adminrole);
    }

    /**
     * 根据后台角色管理id删除后台角色管理
     * @param id
     * @return
     */
    @PostMapping("/adminrole/delete/{id}")
    @PugDoc(name = "根据后台角色管理id删除后台角色管理")
    public int deleteAdminRoleById(@PathVariable("id") String id) {
        Vsserts.isEmptyEx(id, ResultStatusEnum.ID_NOT_EMPTY);
        return adminroleService.deleteAdminRoleById(new Long(id));
    }

    /**
     * 查询角色对用的权限，返回的是id
     * 如果返回的是Long类型会发生精度损失
     * @param adminRoleQueryVo
     * @return
     */
    @PostMapping("/adminrole/findPermission")
    @PugDoc(name = "查询角色对用的权限,返回的是id")
    public List<String> findPermissionByRoleId(@RequestBody @Validated AdminRoleQueryVo adminRoleQueryVo) {
        return adminroleService.findPermissionByRoleId(adminRoleQueryVo.getRoleId());
    }

    /**
     * 给角色授权
     * @return
     */
    @PostMapping("/adminrole/savepermission")
    @PugDoc(name = "给角色授权")
    public Boolean savePermissionByRoleId(@RequestBody @Validated AdminRolePermissionVo adminRolePermissionVo) {
        return adminroleService.savePermissionByRoleId(adminRolePermissionVo.getRoleId(),
                adminRolePermissionVo.getPermissionIds());
    }


}
